package by.dkusch.aircompany.webapp.page.tech.panel;

import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.tables.Plane;
import by.dkusch.aircompany.model.tables.PlaneType;
import by.dkusch.aircompany.model.tables.Plane_;
import by.dkusch.aircompany.services.PlaneService;
import by.dkusch.aircompany.services.PlaneTypeService;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.utils.PlaneTypeChoiseRenderer;
import by.dkusch.aircompany.webapp.page.base.validator.UniqueFieldValidator;

public class PlaneEditPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@Inject
	private PlaneService planeService;

	@Inject
	private PlaneTypeService planeTypeService;

	public PlaneEditPanel(String id, final Plane plane, final PageCreator pageCreator, ActionType action) {
		super(id);

		Form<Plane> form = new Form<Plane>("form", new CompoundPropertyModel<Plane>(plane));

		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.list.name"));
		form.add(tfName);


		final CheckBox chBArc = new CheckBox("arcFl");
		chBArc.add(new PropertyValidator<Boolean>());
		form.add(chBArc);

		form.add(new Label("correctDt"));

		List<PlaneType> planeTypes = planeTypeService.getAll();
		final DropDownChoice<PlaneType> chPlaneType = new DropDownChoice<PlaneType>("planeType", planeTypes, PlaneTypeChoiseRenderer.INSTANCE);
		chPlaneType.add(new PropertyValidator<PlaneType>()).setLabel(new ResourceModel("plane.list.type.code"));

		form.add(chPlaneType);

		final SubmitLink submitLink = new SubmitLink("sumbit-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				super.onSubmit();
				planeService.saveOrUpdate(plane);

				Page page = pageCreator.createPage();
				page.info(getString("column.common.action.saved"));
				setResponsePage(page);
			}

		};

		submitLink.setOutputMarkupId(true);
		form.add(submitLink);

		final TextField<String> tfCode = new TextField<String>("code");
		tfCode.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.list.code"));
		tfCode.add(new UniqueFieldValidator<Long, Plane>(Plane_.code, planeService, plane)).setLabel(new ResourceModel("plane.list.code"));
		form.add(tfCode);

		add(new Link<Void>("back-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
			}

		});

		add(form);
		form.setEnabled(action.equals(ActionType.EDIT));

	}

}
